home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP06 / CHECKER2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  5.6 KB  |  172 lines

  1. /*-------------------------------------------------
  2.    CHECKER2.C -- Mouse Hit-Test Demo Program No. 2
  3.                  (c) Charles Petzold, 1996
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define DIVISIONS 5
  9. #define MoveTo(hdc, x, y) MoveToEx (hdc, x, y, NULL)
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15.      {
  16.      static char szAppName[] = "Checker2" ;
  17.      HWND        hwnd ;
  18.      MSG         msg ;
  19.      WNDCLASSEX  wndclass ;
  20.  
  21.      wndclass.cbSize        = sizeof (wndclass) ;
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  33.  
  34.      RegisterClassEx (&wndclass) ;
  35.  
  36.      hwnd = CreateWindow (szAppName, "Checker2 Mouse Hit-Test Demo",
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.  
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.           {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.           }
  50.      return msg.wParam ;
  51.      }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  54.      {
  55.      static BOOL fState[DIVISIONS][DIVISIONS] ;
  56.      static int  cxBlock, cyBlock ;
  57.      HDC         hdc ;
  58.      PAINTSTRUCT ps ;
  59.      POINT       point ;
  60.      RECT        rect ;
  61.      int         x, y ;
  62.  
  63.      switch (iMsg)
  64.           {
  65.           case WM_SIZE :
  66.                cxBlock = LOWORD (lParam) / DIVISIONS ;
  67.                cyBlock = HIWORD (lParam) / DIVISIONS ;
  68.                return 0 ;
  69.  
  70.           case WM_SETFOCUS :
  71.                ShowCursor (TRUE) ;
  72.                return 0 ;
  73.  
  74.           case WM_KILLFOCUS :
  75.                ShowCursor (FALSE) ;
  76.                return 0 ;
  77.  
  78.           case WM_KEYDOWN :
  79.                GetCursorPos (&point) ;
  80.                ScreenToClient (hwnd, &point) ;
  81.  
  82.                x = max (0, min (DIVISIONS - 1, point.x / cxBlock)) ;
  83.                y = max (0, min (DIVISIONS - 1, point.y / cyBlock)) ;
  84.   
  85.                switch (wParam)
  86.                     {
  87.                     case VK_UP :
  88.                          y-- ;
  89.                          break ;
  90.  
  91.                     case VK_DOWN :
  92.                          y++ ;
  93.                          break ;
  94.  
  95.                     case VK_LEFT :
  96.                          x-- ;
  97.                          break ;
  98.  
  99.                     case VK_RIGHT :
  100.                          x++ ;
  101.                          break ;
  102.  
  103.                     case VK_HOME :
  104.                          x = y = 0 ;
  105.                          break ;
  106.  
  107.                     case VK_END :
  108.                          x = y = DIVISIONS - 1 ;
  109.                          break ;
  110.  
  111.                     case VK_RETURN :
  112.                     case VK_SPACE :
  113.                          SendMessage (hwnd, WM_LBUTTONDOWN, MK_LBUTTON,
  114.                                    MAKELONG (x * cxBlock, y * cyBlock)) ;
  115.                          break ;
  116.                     }
  117.                x = (x + DIVISIONS) % DIVISIONS ;
  118.                y = (y + DIVISIONS) % DIVISIONS ;
  119.  
  120.                point.x = x * cxBlock + cxBlock / 2 ;
  121.                point.y = y * cyBlock + cyBlock / 2 ;
  122.  
  123.                ClientToScreen (hwnd, &point) ;
  124.                SetCursorPos (point.x, point.y) ;
  125.                return 0 ;
  126.  
  127.           case WM_LBUTTONDOWN :
  128.                x = LOWORD (lParam) / cxBlock ;
  129.                y = HIWORD (lParam) / cyBlock ;
  130.  
  131.                if (x < DIVISIONS && y < DIVISIONS)
  132.                     {
  133.                     fState[x][y] ^= 1 ;
  134.  
  135.                     rect.left   = x * cxBlock ;
  136.                     rect.top    = y * cyBlock ;
  137.                     rect.right  = (x + 1) * cxBlock ;
  138.                     rect.bottom = (y + 1) * cyBlock ;
  139.  
  140.                     InvalidateRect (hwnd, &rect, FALSE) ;
  141.                     }
  142.                else
  143.                     MessageBeep (0) ;
  144.                return 0 ;
  145.  
  146.           case WM_PAINT :
  147.                hdc = BeginPaint (hwnd, &ps) ;
  148.  
  149.                for (x = 0 ; x < DIVISIONS ; x++)
  150.                     for (y = 0 ; y < DIVISIONS ; y++)
  151.                          {
  152.                          Rectangle (hdc, x * cxBlock, y * cyBlock,
  153.                                    (x + 1) * cxBlock, (y + 1) * cyBlock) ;
  154.  
  155.                          if (fState [x][y])
  156.                               {
  157.                               MoveTo (hdc,  x    * cxBlock,  y    * cyBlock) ;
  158.                               LineTo (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ;
  159.                               MoveTo (hdc,  x    * cxBlock, (y+1) * cyBlock) ;
  160.                               LineTo (hdc, (x+1) * cxBlock,  y    * cyBlock) ;
  161.                               }
  162.                          }
  163.                EndPaint (hwnd, &ps) ;
  164.                return 0 ;
  165.  
  166.           case WM_DESTROY :
  167.                PostQuitMessage (0) ;
  168.                return 0 ;
  169.           }
  170.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  171.      }
  172.